package com.lewin.ringroad.net.message;

import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 雷达目标.
 *
 * <pre>
 * +───────+─────────────+
 * | enum  | 说明         |
 * +───────+─────────────+
 * | 0     | UNKNOWN     |
 * | 1     | PEDESTRIAN  |
 * | 2     | BIKE        |
 * | 3     | MOTORBIKE   |
 * | 4     | CAR         |
 * | 5     | TRUNK       |
 * | 6     | BUS         |
 * | 7     | LONGTRUNK   |
 * | 8     | MAX         |
 * +───────+─────────────+
 * </pre>
 *
 * @author Jun
 * @since 1.0.0
 */
@Data
public class RadarTargetMessage {
    //@formatter:off

    /** 设备类型 */
    private short type;
    /** 设备 ID */
    private long id;
    /** 时间戳 */
    private long timestamp;
    /** 传输帧号 */
    private short transportFrameCode;
    /** 数据区长度 */
    private int dataLength;
    /** 目标数量 */
    private int targetNumber;
    /** 经度 */
    private double longitude;
    /** 纬度 */
    private double latitude;
    /** 雷达朝向 */
    private float towards;
    /** 目标数组 */
    private List<Target> targets;
    private byte[] crc;

    //@formatter:on


    public static RadarTargetMessage parseFrom(ByteBuf buf) {
        RadarTargetMessage targetMessage = new RadarTargetMessage();
        targetMessage.type = buf.readUnsignedByte();
        targetMessage.id = buf.readLongLE();
        targetMessage.timestamp = buf.readLongLE();
        targetMessage.transportFrameCode = buf.readUnsignedByte();
        targetMessage.dataLength = buf.readUnsignedShortLE();
        targetMessage.targetNumber = buf.readUnsignedShortLE();
        targetMessage.longitude = buf.readDoubleLE();
        targetMessage.latitude = buf.readDoubleLE();
        targetMessage.towards = buf.readFloatLE();
        targetMessage.targets = new ArrayList<>(targetMessage.getTargetNumber());

        for (int i = 0; i < targetMessage.targetNumber; i++) {
            targetMessage.targets.add(Target.parseFrom(buf));
        }

        targetMessage.crc = new byte[2];
        buf.readBytes(targetMessage.crc);
        return targetMessage;
    }


    @Data
    public static class Target {
        //@formatter:off

        /** 目标id */
        private long id;
        /** 目标类型 */
        private short type;

        private double longitude;

        private double latitude;

        private float length;

        private float width;
        /** 目标高度 */
        private float height;
        /** 目标速度 */
        private float speed;
        /** 航向角 */
        private float headingAngle;
        /** 相对距离 */
        private float relativeDistance;
        /** 相对角度 */
        private float relativeAngle;
        /** 目标所处区域 */
        private short area;
        /** 稳定度 */
        private short stability;

        //@formatter:on

        public static Target parseFrom(ByteBuf buf) {
            Target target = new Target();
            target.id = buf.readUnsignedIntLE();
            target.type = buf.readUnsignedByte();
            target.longitude = buf.readDoubleLE();
            target.latitude = buf.readDoubleLE();
            target.length = buf.readFloatLE();
            target.width = buf.readFloatLE();
            target.height = buf.readFloatLE();
            target.speed = buf.readFloatLE();
            target.headingAngle = buf.readFloatLE();
            target.relativeDistance = buf.readFloatLE();
            target.relativeAngle = buf.readFloatLE();
            target.area = buf.readUnsignedByte();
            target.stability = buf.readUnsignedByte();

            return target;
        }
    }
}
